package com.honda.util;

import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {
	private static final ObjectMapper MAPPER = new ObjectMapper();

	/***
	 * 
	 * @Title: objectToJson   
	 * @Description: (object转json)   
	 * @param: @param data
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String objectToJson(Object data) {
		try {
			String string = MAPPER.writeValueAsString(data);
			return string;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @Title: jsonToPojo   
	 * @Description: (JSON转POJO)   
	 * @param: @param jsonData
	 * @param: @param beanType
	 * @param: @return      
	 * @return: T      
	 * @throws
	 */
	public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
		try {
			T t = MAPPER.readValue(jsonData, beanType);
			return t;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @Title: jsonToList   
	 * @Description: (json字符串转List)   
	 * @param: @param jsonData
	 * @param: @param beanType
	 * @param: @return      
	 * @return: List<T>      
	 * @throws
	 */
	public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
		JavaType javaType = MAPPER.getTypeFactory().constructParametricType(
				List.class, beanType);
		try {
			List<T> list = MAPPER.readValue(jsonData, javaType);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

}
